﻿Imports System.IO
Public Class weeks
    Private Sub weeks_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadWeeks(first:=True)
    End Sub
    Private Sub loadWeeks(first As Boolean)
        On Error Resume Next
        Button1.Hide()
        ListBox1.SelectedItems.Clear() ' Must remove all items, or loadweeks() will just add to the list, not 'reload' it. 
        Dim folderInfo As New IO.DirectoryInfo("week/data")
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.*")
        For Each fileInFolder In arrFilesInFolder
            ListBox1.Items.Add(fileInFolder.Name)
        Next
        If first = False Then
            ListBox1.SetSelected(0, True)
        End If
        If Err.Number <> 0 Then
            MsgBox("A error occured:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, "DRSS Homework Manager")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'On Error Resume Next
        If TextBox1.Text = "" Then
            MsgBox("Enter something in the week name box.")
            Exit Sub
        End If
        Dim weekname As String
        'Dim textm1 As String
        weekname = TextBox1.Text

        'Now copy the files to the new location based on user input to text box
        'But first create sub dir in week folder
        If Directory.Exists("week/" + weekname) Then

        Else
            Directory.CreateDirectory("week/" + weekname)
        End If
        Dim copyloc As String
        copyloc = "week/" + weekname
        '        Directory   textinput      textinput           Creates a string with week/usersweekname/
        If File.Exists("week/data/" + weekname) Then
            MsgBox("Sorry, the week name" + weekname + "already exists")
            Me.Close()
        Else
            File.Create("week/data/" + weekname)

        End If

        File.Copy("week/dump/P1Monday.kwl", copyloc + "/P1Monday.kwl")
        File.Copy("week/dump/P2Monday.kwl", copyloc + "/P2Monday.kwl")
        File.Copy("week/dump/P3Monday.kwl", copyloc + "/P3Monday.kwl")
        File.Copy("week/dump/P4Monday.kwl", copyloc + "/P4Monday.kwl")
        File.Copy("week/dump/P5Monday.kwl", copyloc + "/P5Monday.kwl")
        File.Copy("week/dump/P6Monday.kwl", copyloc + "/P6Monday.kwl")
        File.Copy("week/dump/P1Tusday.kwl", copyloc + "/P1Tusday.kwl")
        File.Copy("week/dump/P2Tusday.kwl", copyloc + "/P2Tusday.kwl")
        File.Copy("week/dump/P3Tusday.kwl", copyloc + "/P3Tusday.kwl")
        File.Copy("week/dump/P4Tusday.kwl", copyloc + "/P4Tusday.kwl")
        File.Copy("week/dump/P5Tusday.kwl", copyloc + "/P5Tusday.kwl")
        File.Copy("week/dump/P6Tusday.kwl", copyloc + "/P6Tusday.kwl")
        File.Copy("week/dump/P1Wednesday.kwl", copyloc + "/P1Wednesday.kwl")
        File.Copy("week/dump/P2Wednesday.kwl", copyloc + "/P2Wednesday.kwl")
        File.Copy("week/dump/P3Wednesday.kwl", copyloc + "/P3Wednesday.kwl")
        File.Copy("week/dump/P4Wednesday.kwl", copyloc + "/P4Wednesday.kwl")
        File.Copy("week/dump/P5Wednesday.kwl", copyloc + "/P5Wednesday.kwl")
        File.Copy("week/dump/P6Wednesday.kwl", copyloc + "/P6Wednesday.kwl")
        File.Copy("week/dump/P1Thursday.kwl", copyloc + "/P1Thursday.kwl")
        File.Copy("week/dump/P2Thursday.kwl", copyloc + "/P2Thursday.kwl")
        File.Copy("week/dump/P3Thursday.kwl", copyloc + "/P3Thursday.kwl")
        File.Copy("week/dump/P4Thursday.kwl", copyloc + "/P4Thursday.kwl")
        File.Copy("week/dump/P5Thursday.kwl", copyloc + "/P5Thursday.kwl")
        File.Copy("week/dump/P6Thursday.kwl", copyloc + "/P6Thursday.kwl")
        File.Copy("week/dump/P1Friday.kwl", copyloc + "/P1Friday.kwl")
        File.Copy("week/dump/P2Friday.kwl", copyloc + "/P2Friday.kwl")
        File.Copy("week/dump/P3Friday.kwl", copyloc + "/P3Friday.kwl")
        File.Copy("week/dump/P4Friday.kwl", copyloc + "/P4Friday.kwl")
        File.Copy("week/dump/P5Friday.kwl", copyloc + "/P5Friday.kwl")
        File.Copy("week/dump/P6Friday.kwl", copyloc + "/P6Friday.kwl")
        File.Copy("week/dump/Notes.kwl", copyloc + "/Notes.kwl")

        ListBox1.Items.Clear()
        Dim folderInfo As New IO.DirectoryInfo("week/data")
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.*")
        For Each fileInFolder In arrFilesInFolder
            ListBox1.Items.Add(fileInFolder.Name)
        Next

        'If Err.Number <> 0 Then
        'MsgBox("A error occured:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, "DRSS Homework Manager")
        ' End If

    End Sub
    Private Sub ending() Handles Me.FormClosing
        
    End Sub
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        changeSelect()
    End Sub
    Public Sub changeSelect()
        On Error Resume Next
        If KHM.Main.Button8.Visible Then
            If MessageBox.Show("You have modifed this page. Do you want to save the page before swiching?!", "Save", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                KHM.Main.Button8.PerformClick()
                If Err.Number <> 0 Then
                    MsgBox("Something is wrong with your computer:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, )
                Else
                End If

            End If
        End If
        Dim selection As String
        selection = ListBox1.SelectedItem

        Dim objWriter As New StreamWriter("week/select.kwl")
        objWriter.Write(selection)
        objWriter.Close()

        Dim weekselect As String
        weekselect = File.ReadAllText("week/select.kwl")
        objWriter.Close()
        KHM.Main.Label42.Text = weekselect
        KHM.Main.reload(saveB:=True)
        If Err.Number <> 0 Then
            MsgBox("A error occured:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, "DRSS Homework Manager")
        End If
    End Sub
    Dim selection As String  ' VERY important!!!
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        On Error Resume Next
        selection = ListBox1.SelectedItem
        File.Delete("week/data/" + selection)
        File.Delete("week/" + selection + "/P1Monday.kwl")
        File.Delete("week/" + selection + "/P2Monday.kwl")
        File.Delete("week/" + selection + "/P3Monday.kwl")
        File.Delete("week/" + selection + "/P4Monday.kwl")
        File.Delete("week/" + selection + "/P5Monday.kwl")
        File.Delete("week/" + selection + "/P6Monday.kwl")
        File.Delete("week/" + selection + "/P1Tusday.kwl")
        File.Delete("week/" + selection + "/P2Tusday.kwl")
        File.Delete("week/" + selection + "/P3Tusday.kwl")
        File.Delete("week/" + selection + "/P4Tusday.kwl")
        File.Delete("week/" + selection + "/P5Tusday.kwl")
        File.Delete("week/" + selection + "/P6Tusday.kwl")
        File.Delete("week/" + selection + "/P1Wednesday.kwl")
        File.Delete("week/" + selection + "/P2Wednesday.kwl")
        File.Delete("week/" + selection + "/P3Wednesday.kwl")
        File.Delete("week/" + selection + "/P4Wednesday.kwl")
        File.Delete("week/" + selection + "/P5Wednesday.kwl")
        File.Delete("week/" + selection + "/P6Wednesday.kwl")
        File.Delete("week/" + selection + "/P1Thursday.kwl")
        File.Delete("week/" + selection + "/P2Thursday.kwl")
        File.Delete("week/" + selection + "/P3Thursday.kwl")
        File.Delete("week/" + selection + "/P4Thursday.kwl")
        File.Delete("week/" + selection + "/P5Thursday.kwl")
        File.Delete("week/" + selection + "/P6Thursday.kwl")
        File.Delete("week/" + selection + "/P1Friday.kwl")
        File.Delete("week/" + selection + "/P2Friday.kwl")
        File.Delete("week/" + selection + "/P3Friday.kwl")
        File.Delete("week/" + selection + "/P4Friday.kwl")
        File.Delete("week/" + selection + "/P5Friday.kwl")
        File.Delete("week/" + selection + "/P6Friday.kwl")
        File.Delete("week/" + selection + "/Notes.kwl")
        Directory.Delete("week/" + selection)
        ListBox1.Items.Clear()
        Dim folderInfo As New IO.DirectoryInfo("week/data")
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.*")
        For Each fileInFolder In arrFilesInFolder
            ListBox1.Items.Add(fileInFolder.Name)
        Next
        ListBox1.SetSelected(0, True)
        If Err.Number <> 0 Then
            MsgBox("A error occured:" & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, "DRSS Homework Manager")
        End If
    End Sub

    Private Sub Msgbox_Yes_No()

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Button1.Show()
    End Sub
   
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        On Error Resume Next
        selection = ListBox1.SelectedItem
        If TextBox1.Text = "" Then
            MsgBox("You need to insert the selection's new name into the new week textbox.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        If TextBox1.Text = ListBox1.SelectedItem.ToString Then
            MsgBox("To rename weeks, enter a different name in the box on the right (the same one used to add weeks), select a week, and press rename.", MsgBoxStyle.Exclamation)
        End If
        File.Move("week\data\" + selection, "week\data\" + TextBox1.Text)
        Directory.Move("week\" + selection, "week\" + TextBox1.Text)
        TextBox1.Text = ""

        ListBox1.Items.Clear()
        Dim folderInfo As New IO.DirectoryInfo("week/data")
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.*")
        For Each fileInFolder In arrFilesInFolder
            ListBox1.Items.Add(fileInFolder.Name)
        Next
        ListBox1.SetSelected(0, True)
        changeSelect()
        If Err.Number <> 0 Then
            MsgBox("A error occured. Please email kowal@innocent.com with a description of what you were doing and this:" + vbNewLine & vbCrLf & Err.Description & vbCrLf & Err.Source, vbOKOnly - vbExclamation, "DRSS Homework Manager")
        End If
    End Sub
End Class
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.

'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>